package com.kiki.stater.test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by mazeyi on 2020/12/18.
 */
public class Demo {

    public static void main(String[] args) {
        try {
            Apple apple = new Apple();
            apple.setAge(11);
            apple.setName("富士康");
            apple.setDescription("1111111");
            Class clz = apple.getClass();

            List<Field> fieldList = new ArrayList<>();
            Collections.addAll(fieldList, clz.getDeclaredFields());
            while (clz.getSuperclass() != null){
                clz = clz.getSuperclass();
                Collections.addAll(fieldList, clz.getDeclaredFields());
            }
            for(Field f : fieldList){
                Field field = f;
                //打开私有访问
                field.setAccessible(true);
                //获取属性
                String name = field.getName();
                //获取属性值
                System.out.println(field.get(apple));
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}